package com.xca.gsondemo.views;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.bumptech.glide.Glide;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.xca.gsondemo.R;
import com.xca.gsondemo.entity.Student;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private BottomSheetDialog bottomSheetDialog ;
    private static final String TAG = "MainActivity -- Log" ;
    private List<Student> studentList = new ArrayList<>();
    private TextView mShow ;
    private Student mStudent ;
    private static final int CH_TYPE = 1 ;
    private ImageButton toCh ;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toCh = findViewById(R.id.choose);

//        mShow = findViewById(R.id.show);
//
//        String gsonStr = toGson() ;
//        Log.d(TAG, gsonStr);
//        mStudent = fromToGson();
//        mShow.setText(gsonStr +",\n" + mStudent.toString());
        replaceFragment(new BookFragment());

        toCh.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.choose){
            showBottom();
            Log.d("00000000", "选择按钮已被点击");
        }
    }
    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frame , fragment);
        transaction.commit() ;
    }

    private void showBottom(){
        bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
        bottomSheetDialog.setContentView(R.layout.choose);

        ImageView toBook = bottomSheetDialog.findViewById(R.id.toBook);
        ImageView toGold = bottomSheetDialog.findViewById(R.id.toGold);
        ImageView toHot = bottomSheetDialog.findViewById(R.id.toHot);

        assert toBook != null;
        toBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new BookFragment());
                bottomSheetDialog.dismiss();
            }
        });
        toGold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new GoldFragment());
                bottomSheetDialog.dismiss();
            }
        });
        toHot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new HotFragment());
                bottomSheetDialog.dismiss();
            }
        });
        bottomSheetDialog.show();
    }

//    private String toGson(){
//        Student student = new Student();
//        student.setId(1);
//        student.setName("lisa");
//        student.setCourse("2021");
//        student.setSex("女");
//        student.setAge(18);
//        studentList.add(student);
//        Gson gson = new Gson();
//        String gsonStu = gson.toJson(student);
//        return gsonStu ;
//    }
//   private Student fromToGson(){
//        String str = toGson() ;
//        Gson gson = new Gson() ;
//        Student student = gson.fromJson(str , Student.class) ;
//        return student ;
//   }
}